import request from "../utils/request";

export interface NotificationRequest {
  type?: string;
  priority?: number;
  isRead?: number;
  page?: number;
  size?: number;
  keyword?: string;
}

export interface NotificationStats {
  total: number;
  unread: number;
  system: number;
  task: number;
  exam: number;
  assignment: number;
  approval: number;
}

// 构建查询参数字符串
const buildQueryParams = (params: Record<string, any>): string => {
  const searchParams = new URLSearchParams();
  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined && value !== null && value !== "") {
      searchParams.append(key, String(value));
    }
  });
  const queryString = searchParams.toString();
  return queryString ? `?${queryString}` : "";
};

export const notificationApi = {
  // 获取通知列表
  getNotifications: (params: NotificationRequest) => {
    const queryParams = buildQueryParams(params);
    return request.get(`/api/v1/notification/list${queryParams}`);
  },

  // 获取通知统计信息
  getStats: () => {
    return request.get("/api/v1/notification/stats");
  },

  // 标记通知为已读
  markAsRead: (ids: number[]) => {
    return request.post("/api/v1/notification/read", { ids });
  },

  // 删除通知
  deleteNotifications: (ids: number[]) => {
    return request.post("/api/v1/notification/delete", { ids });
  },

  // 标记所有通知为已读
  markAllAsRead: () => {
    return request.post("/api/v1/notification/read-all");
  },

  // 获取未读通知数量
  getUnreadCount: () => {
    return request.get("/api/v1/notification/unread-count");
  },

  // 生成AI建议
  generateAISuggestion: (notificationId: number) => {
    return request.post(`/api/v1/notification/${notificationId}/ai-suggestion`);
  },

  // 更新通知设置
  updateSettings: (settings: any) => {
    return request.post("/api/v1/notification/settings", settings);
  },

  // 获取通知设置
  getSettings: () => {
    return request.get("/api/v1/notification/settings");
  },
};
